package core

class AssignmentOneController {

	def index() {

		//link(view:'index','go to AssignmentOneController')
		//[emp: Person.findByFirstNameLike(params.name),na: params.name]
render  text:"""<pre>
${link(action: 'oneFind', '1.1- Find the oldest person using Daynamic Finders')}
${link(action: 'oneWhere', '1.2- Find the oldest person using Where')}
${link(action: 'oneCriteria', '1.3- Find the oldest person using Criteria')}

${link(action: 'threeFind','3.1- Find Persons that have IDs or Birth Certificates using Dynamic Finders')}
${link(action: 'threeWhere', '3.2- Find Persons that have IDs or Birth Certificates using Where')}
${link(action: 'threeCriteria','3.3- Find Persons that have IDs or Birth Certificates using Criteria')}

${link(action: 'fourFind','4.1- Find Persons that are younger than the youngest one with lastName =”Ali” using Dynamic Finders')}
${link(action: 'fourWhere','4.2- Find Persons that are younger than the youngest one with lastName = ”Ali” using where')}
${link(action: 'fourCriteria','4.3- Find Persons that are younger than the youngest one with lastName = ”Ali” using Criteria')}

${link(action: 'fiveFind','5.1- Find First two persons older than the oldest one with lastName = “Nazzal” (sorted by firstName asending) Dynamic Finders')}
${link(action: 'fiveWhere','5.2- Find First two persons older than the oldest one with lastName = “Nazzal” (sorted by firstName asending) using where')}
${link(action: 'fiveCriteria','5.3- Find First two persons older than the oldest one with lastName = “Nazzal” (sorted by firstName asending) using Criteria')}
</pre>
"""
	}
	def oneFind () {
		def oldest = Person.findAllByBirthDateGreaterThan(new Date('1900/1/1'),[sort:'birthDate', order:'asc'])
		def map = [person: oldest[0],'question':"Find the oldest person using Dynamic Finders"]
		render(view: 'oneFind', model : map)
		}

	def oneWhere() {
		def q = Person.where {
			(birthDate <= min(birthDate))
		}
		def map = [person: q.find()]
		render(view: 'oneWhere', model: map)
	}

	def oneCriteria(){
		def query = Person.createCriteria()
		def oldest = query.list {
			order('birthDate','asc')
		}
		
		def map = [person: oldest[0]]
		render(view:'oneCriteria',model: map)
	}
//============== Three related actions =====================	
	def threeFind () {
		//def li = LegalIdentifier.FindByDocumentTypeIlikeOrDocumentTypeIlike('cer%','id%')
		render(view: 'threeFind')
		
	}

	def threeWhere() {
		def q = Person.where{
			legalIdentifiers.DocumentType.name =~ 'cert%' || legalIdentifiers.DocumentType.name =~ 'id%'
			}
		
		def map = [person: q.findAll()]
		render(view: 'threeWhere',model: map)
	}
	
	def threeCriteria(){
		def q = Person.createCriteria()
		def l = q.listDistinct {

			legalIdentifiers {
				documentType {
					or{
						ilike('name', 'id%')
						ilike('name','cert%')
					}
				}
			}
		}
		def map = [person:l]
		render(view: 'threeCriteria',model: map)
	}
	
	
//=============== Four related actions ======================	
	def fourFind () {
		//Persons that are younger than the youngest one with lastName==”Ali”
		def l = Person.findAllByLastNameLike('Ali',[sort:'birthDate', order:'desc',max:1,offset:0])
		def l2 = Person.findAllByBirthDateGreaterThan(l[0].birthDate)
		def map = [person: l2]
		render(view: 'fourFind',model: map)
	}
	
	def fourWhere() {
		def q = Person.where {
			lastName == "Ali"
				}
		def l = q.list(max:1,offset:0,sort:'birthDate',order:'desc')
		def q2 = Person.where {
			birthDate > l[0].birthDate
		}
		def map = [person: q2.list()]
		render(view: 'fourWhere', model:map)
	}
	def fourCriteria(){
		def q = Person.createCriteria()
		def l = q.list{
			/* min('birthDate') works perfect*/
			eq('lastName','Ali')
			order('birthDate','desc')
			firstResult (0)
			maxResults(1)
			}
		def l2 = Person.findAll{
			gt('birthDate',l[0].birthDate)
		}
	def map = [person: l2]
	
		render(view: 'fourCriteria', model: map)
	}
	
//================ Five Related actions ======================
	
	def fiveFind (){
		def q = Person.findAllByLastName('Nazzal',[sort: 'birthDate',order:'asc'])
		def q2 = Person.findAllByBirthDateLessThan(q[0].birthDate, [sort: 'firstName',order:'asc',max:2, offset:0])
		def map = [person: q2]
		render(view: 'fiveFind', model: map)
		//render text:"${q2}"
	}

	def fiveWhere () {
		def q = Person.where { lastName == "Nazzal" }
		def l = q.list(sort:'birthDate',order:'asc',offset:0, max:1)

		def q3 = Person.where {
			birthDate < l[0].birthDate
		}
		def l2 = q3.list(sort:'birthDate',order:'asc',offset:0, max:2)
		def map = [person: l2]
		render(view: 'fiveWhere', model: map)
		//render text:"$l2"
	}
	def fiveCriteria(){
		def q = Person.createCriteria()
		def l = q.list{
			eq('lastName', 'Nazzal')
			order('birthDate','asc')
			firstResult(0)
			maxResults (1)
		}
		def q2 = Person.createCriteria()
		def l2 = q2.list {
			lt('birthDate', l[0].birthDate)
			order('firstName','asc')
			firstResult(0)
			maxResults(2)
		}
		def map = [person: l2]
		render(view:'fiveCriteria',model: map)
		//render text: "$l2"
	}
}
